<?php

namespace Home\Controller;

/**
 * 我的账目
 *
 * @author Administrator
 *        
 */
class MyAccountController extends BaseController {
	/**
	 * 获取我的账目
	 */
	public function mb_GetMyAccount() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$userInfo = M ( 'Userinfo' );
		$userRechargeLog = M ( 'Userrechargelog' );
		$userBetLog = M ( 'Userbetlog' );
		$applyCashLog = M ( 'Applycashlog' );
		if ($adminData ['level'] == 0) {
			// 总管理员
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 1) {
			// 平台上分
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 2) {
			// 一级代理
			$name = $adminData ['name'];
			$code = mb_substr ( $adminData ['code'], 0, 5 );
			$applyCashData = $applyCashLog->where ( "agentname='$name'" )->order ( 'id desc' )->find ();
			if (isset ( $applyCashData )) {
				$date = $applyCashData ['date'];
				$where = "date>'$date' AND code like '$code" . "___'";
			} else {
				$where = "code like '$code" . "___'";
			}
			$coins = $userInfo->where ( "code like '$code" . "___'" )->sum ( 'coin' );
		} else if ($adminData ['level'] == 3) {
			// 二级代理
			$name = $adminData ['name'];
			$code = $adminData ['code'];
			$applyCashData = $applyCashLog->where ( "agentname='$name'" )->order ( 'id desc' )->find ();
			if (isset ( $applyCashData )) {
				$date = $applyCashData ['date'];
				$where = "date>'$date' AND code='$code'";
			} else {
				$where = "code='$code'";
			}
			$coins = $userInfo->where ( "code='$code'" )->sum ( 'coin' );
		}
		
		if ($adminData ['period'] == - 1) {
			$period = '可提现';
		} else {
			$day = $adminData ['period'];
			$targetDate = date ( 'Y-m-d H:i:s', strtotime ( "+$day day", strtotime ( $date ) ) );
			if (\Tools::mb_GetTimeDifference ( $targetDate, \Tools::mb_GetCurDateTime () ) > 0) {
				$period = "于 $targetDate 后可提现！";
			} else {
				$period = "可提现";
			}
		}
		
		// $handlerName = $name;
		// if ($where != '')
		// $where .= ' AND ';
		
		// $adminCode = $adminData ['code'];
		// $up = $userRechargeLog->where ( $where . "code='$adminCode' AND type='0'" )->sum ( 'coins' );
		// $down = $userRechargeLog->where ( $where . "code='$adminCode' AND type='1'" )->sum ( 'coins' );
		// $send = $userRechargeLog->where ( $where . "code='$adminCode' AND type='2'" )->sum ( 'coins' );
		// $bet = $userBetLog->where ( $where . "code='$adminCode'" )->sum ( 'bets' );
		// $coins = $userInfo->where ( "code='$adminCode'" )->sum ( 'coin' );
		
		$up = $userRechargeLog->where ( $where . " AND type='0'" )->sum ( 'coins' );
		$down = $userRechargeLog->where ( $where . " AND type='1'" )->sum ( 'coins' );
		$send = $userRechargeLog->where ( $where . " AND type='2'" )->sum ( 'coins' );
		$bet = $userBetLog->where ( $where )->sum ( 'bets' );
		
		if ($up == '')
			$up = 0;
		if ($down == '')
			$down = 0;
		if ($send == '')
			$send = 0;
		if ($bet == '')
			$bet = 0;
		if ($coins == '')
			$coins = 0;
		
		$profitMode = $adminData ['profitmode'];
		
		if ($profitMode == 0) {
			$profitRate = $adminData ['profitrate'] / 1000;
			$profit = ($up + $down - $coins) * $profitRate;
			$profit = round ( $profit, 2 );
			$rateStr = $adminData ['profitrate'] . " ‰";
		} else if ($profitMode == 1) {
			$betRate = $adminData ['betrate'] / 1000;
			$profit = $bet * $betRate;
			$profit = round ( $profit, 2 );
			$rateStr = $adminData ['betrate'] . " ‰";
		} else if ($profitMode == 2) {
			$profitRate = $adminData ['profitrate'] / 1000;
			$betRate = $adminData ['betrate'] / 1000;
			$profit = ($up + $down - $coins) * $profitRate + $bet * $betRate;
			$rateStr = $adminData ['profitrate'] . '‰ | ' . $adminData ['betrate'] . '‰';
			$profit = round ( $profit, 2 );
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( array (
				'up' => ( int ) $up,
				'down' => ( int ) $down,
				'send' => ( int ) $send,
				'coins' => ( int ) $coins,
				'bet' => ( int ) $bet,
				'profitmode' => $profitMode,
				'profitrate' => $rateStr,
				'profit' => $profit,
				'period' => $period,
				'date' => \Tools::mb_GetCurDateTime () 
		) ) );
	}
	/**
	 * 提现
	 */
	public function mb_SubmitApplyCash() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$money = $data->money;
		$date = $data->date;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		if ($money <= 0) {
			$this->mb_OutMessage ( ErrCode_Toast, '提现的金额不足' );
			return;
		}
		
		$applyCashLog = M ( 'Applycashlog' );
		$name = $adminData ['name'];
		$applyCashData = $applyCashLog->where ( "agentname='$name'" )->find ();
		if ($applyCashData != null) {
			if (\Tools::mb_GetTimeDifference ( \Tools::mb_GetCurDateTime (), $applyCashData ['date'] ) <= ($adminData ['period'] * 86400)) {
				$this->mb_OutMessage ( ErrCode_Toast, '您的提现周期还没到，请耐心等候' );
				return;
			}
		}
		if ($adminData ['level'] == 0) {
			// 总管理员
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 1) {
			// 平台上分
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 2) {
			// 一级代理
			$handlerName = C ( 'ADMIN_NAME' );
			$handlerCode = '1';
		} else if ($adminData ['level'] == 3) {
			// 二级代理
			// $subCode = mb_substr ( $adminData ['code'], 0, 5 ) . '000';
			$agentData = $adminInfo->where ( "code like '$code" . "___' AND level = '2'" )->find ();
			$handlerName = $agentData ['name'];
			$handlerCode = $agentData ['code'];
		}
		
		$applyCashLog->add ( array (
				'agentname' => $adminData ['name'],
				'agentcode' => $adminData ['code'],
				'handlername' => $handlerName,
				'handlercode' => $handlerCode,
				'applymoney' => $money,
				'profitmodel' => $adminData ['profitmode'],
				'profitrate' => $adminData ['profitrate'],
				'betrate' => $adminData ['betrate'],
				'date' => $date 
		) );
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 获取提现列表
	 */
	public function mb_GetApplyCashList() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$name = $adminData ['name'];
		
		$applyCashLog = M ( 'Applycashlog' );
		if ($adminData ['level'] == 0) {
			// 总管理员
			// $handlerName = C ( 'ADMIN_NAME' );
			// $applyCashData = $applyCashLog->where ( "handlername='$handlerName'" )->select ();
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 1) {
			// 平台上分
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 2) {
			// 一级代理
			$applyCashData = $applyCashLog->where ( "agentname='$name'" )->order ( 'id desc' )->select ();
		} else if ($adminData ['level'] == 3) {
			// 二级代理
			$applyCashData = $applyCashLog->where ( "agentname='$name'" )->order ( 'id desc' )->select ();
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $applyCashData ) );
	}
	/**
	 */
	public function mb_GetHandleCashList() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$name = $adminData ['name'];
		
		$applyCashLog = M ( 'Applycashlog' );
		if ($adminData ['level'] == 0) {
			// 总管理员
			$handlerName = C ( 'ADMIN_NAME' );
			$applyCashData = $applyCashLog->where ( "handlername='$handlerName'" )->select ();
		} else if ($adminData ['level'] == 1) {
			// 平台上分
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 2) {
			// 一级代理
			$applyCashData = $applyCashLog->where ( "handlername='$name'" )->select ();
		} else if ($adminData ['level'] == 3) {
			// 二级代理
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $applyCashData ) );
	}
	/**
	 */
	public function mb_HandleApplyCash() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$logId = $data->id;
		$money = $data->money;
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$name = $adminData ['name'];
		$applyCashLog = M ( 'Applycashlog' );
		$applyCashLog->save ( array (
				'id' => $logId,
				'status' => 1,
				'actualmoney' => $money 
		) );
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 获取旗下代理未提交申请列表
	 */
	public function mb_GetUnApplyList() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		if ($adminData ['level'] != 0 && $adminData ['level'] != 2) {
			$this->mb_OutMessage ( ErrCode_Dialog, '您没有该权限查看' );
			return;
		}
		
		$applyCashLog = M ( 'Applycashlog' );
		$userRechargeLog = M ( 'Userrechargelog' );
		$userBetLog = M ( 'Userbetlog' );
		$userInfo = M ( 'Userinfo' );
		$sendData = array ();
		
		if ($adminData ['level'] == 0)
			$agentDatas = $adminInfo->where ( "level='2'" )->select ();
		else if ($adminData ['level'] == 2)
			$agentDatas = $adminInfo->where ( "level='3'" )->select ();
		
		for($i = 0; $i < count ( $agentDatas ); $i ++) {
			$name = $agentDatas [$i] ['name'];
			$applyCashData = $applyCashLog->where ( "agentname='$name'" )->order ( 'id desc' )->find ();
			if (isset ( $applyCashData )) {
				$date = $applyCashData ['date'];
				$where = "date>'$date'";
			} else {
				$where = '';
			}
			
			if ($agentDatas [$i] ['period'] == - 1) {
				$period = '可提现';
			} else {
				$day = $agentDatas [$i] ['period'];
				$targetDate = date ( 'Y-m-d H:i:s', strtotime ( "+$day day", strtotime ( $date ) ) );
				if (\Tools::mb_GetTimeDifference ( $targetDate, \Tools::mb_GetCurDateTime () ) > 0) {
					$period = "于 $targetDate 后可提现！";
				} else {
					$period = "可提现";
				}
			}
			
			if ($where != '')
				$where .= ' AND ';
			
			$agentCode = $agentDatas [$i] ['code'];
			$up = $userRechargeLog->where ( $where . "code='$agentCode' AND type='0'" )->sum ( 'coins' );
			$down = $userRechargeLog->where ( $where . "code='$agentCode' AND type='1'" )->sum ( 'coins' );
			$send = $userRechargeLog->where ( $where . "code='$agentCode' AND type='2'" )->sum ( 'coins' );
			$bet = $userBetLog->where ( $where . "code='$agentCode'" )->sum ( 'bets' );
			$coins = $userInfo->where ( "code='$agentCode'" )->sum ( 'coin' );
			
			if ($up == '')
				$up = 0;
			if ($down == '')
				$down = 0;
			if ($send == '')
				$send = 0;
			if ($bet == '')
				$bet = 0;
			if ($coins == '')
				$coins = 0;
			
			$profitMode = $agentDatas [$i] ['profitmode'];
			
			if ($profitMode == 0) {
				$profitRate = $agentDatas [$i] ['profitrate'] / 1000;
				$profit = ($up + $down - $coins) * $profitRate;
				$profit = round ( $profit, 2 );
				$rateStr = $agentDatas [$i] ['profitrate'] . " ‰";
			} else if ($profitMode == 1) {
				$betRate = $agentDatas [$i] ['betrate'] / 1000;
				$profit = $bet * $betRate;
				$profit = round ( $profit, 2 );
				$rateStr = $agentDatas [$i] ['betrate'] . " ‰";
			} else if ($profitMode == 2) {
				$profitRate = $agentDatas [$i] ['profitrate'] / 1000;
				$betRate = $agentDatas [$i] ['betrate'] / 1000;
				$profit = ($up + $down - $coins) * $profitRate + $bet * $betRate;
				$rateStr = $agentDatas [$i] ['profitrate'] . '‰ | ' . $agentDatas [$i] ['betrate'] . '‰';
				$profit = round ( $profit, 2 );
			}
			$sendData [] = array (
					'name' => $agentDatas [$i] ['name'],
					'up' => ( int ) $up,
					'down' => ( int ) $down,
					'send' => ( int ) $send,
					'coins' => ( int ) $coins,
					'bet' => ( int ) $bet,
					'profitmode' => $profitMode,
					'profitrate' => $rateStr,
					'profit' => $profit,
					'period' => $period,
					'date' => \Tools::mb_GetCurDateTime () 
			);
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $sendData ) );
	}
}